<template>
  <div class="employees">
    <div class="per-container">
      <div class="per-header">
        <p class="per-left el-icon-edit">共计{{page.total}}条记录</p>
        <div class="per-right">
        <el-button type="primary" @click="$router.push('/import')">excel导入</el-button>
        <el-button type="primary" @click="exportData">excel导出</el-button>
        <el-button type="primary">新增员工</el-button>
        </div>
      </div>
      <el-table class="em-main" :data="list">
        <el-table-column type="index" label="序号" sortable="" width="60px" />
        <el-table-column label="姓名" prop="username" sortable="" width="120px" />
        <el-table-column width="130px" label="头像" align="center">
          <template v-slot="{ row }">
            <img
              v-imagerror="require('@/assets/common/head.jpg')"
              :src="row.staffPhoto ? row.staffPhoto : '@/assets/common/head.jpg'"
              alt=""
              style="border-radius: 20%; width: 80px; height: 80px;"
            >
          </template>
        </el-table-column>
        <el-table-column label="工号" prop="workNumber" sortable="" width="100px" />
        <el-table-column label="聘用形式"  sortable="" width="100px" prop="formOfEmployment" :formatter="formatEmployment" />
        <el-table-column label="部门" prop="departmentName" sortable="" width="100px" />
        <el-table-column label="入职时间" prop="timeOfEntry" sortable="" width="150px">
          <template v-slot="{ row }">
          </template>
        </el-table-column>
        <el-table-column label="账户状态" prop="enableState" sortable="" width="100px">
          <template v-slot="{ row }">
            <el-switch :value="row.enableState === 1" />
          </template>
        </el-table-column>
        <el-table-column label="操作" sortable="" fixed="right" width="320px">
          <template v-slot="{ row }">
            <el-button type="text" size="medium" @click="$router.push(`/employees/staff/${row.id}`)">查看</el-button>
            <el-button type="text" size="medium" >角色</el-button>
            <el-button type="text" size="medium" @click="delUsers(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          :current-page="page.page"
          :page-size="page.size"
          :total="page.total"
          layout="prev, pager, next"
          @current-change="changePage"
        />
      </el-row>
  </div>
  </div>
</template>

<script>
import {getUsers, getEmployeeList, delUsers} from '@/api/employees'
import { formatDate } from '@/filters'
import EmployeeEnum from '@/api/constant/employees'
export default {
  name: 'employees',
  components: {
  },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
      }
    }
  },
  created(i) {
    this.getUsers()
  },
  methods: {
    async getUsers() {
      const { total, rows } = await getUsers(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage(newPage) {
      this.page.page = newPage // 赋值最新页码
      this.getUsers() // 重新拉取数据
    },
    exportData(){
      const headers = {
        '手机号': 'mobile',
        '姓名': 'username',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'orrectionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      // 导出excel
      import('@/vendor/Export2Excel').then(async excel =>{
        // excel是引入文件的导出对象
        const { rows } = await getEmployeeList({page: 1,size: this.page.total })
        const data = this.formatJson(headers, rows) //返回的data就是要导出的结构
        const multiHeader = [['姓名','主要信息','','','','','部门']]
        const merges = ['A1:A2','B1:F1','G1:G2']
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          filename:'员工资料表',
          multiHeader,
          merges
        })
        // excel.export_json_to_excel({
        //   header: ["姓名","工资"],//表头
        //   data: [],//具体数据
        //   filename: "员工工资表",//导出文件名
        // })
      })
    },
    // 将表头数据和数据进行对应
    formatJson(headers, rows) {
      return rows.map(item => {
        // item是一个对象
        // ["手机号","姓名","入职日期"]
        return Object.keys(headers).map(key => {
          // 需要判断字段
          if(headers[key] === 'timeOfEntry' || headers[key] === 'correctionTime') {
            // 格式化日期
            return formatDate(item[headers[key]])
          } else if(headers[key] === 'formOfEmployment'){
            const obj = EmployeeEnum.hireType.find(obj => obj.id === item[headers[key]])
            return obj ? obj.value : '未知'
          }
          return item[headers[key]]
        })
      })
    },
    async delUsers(id) {
      try {
        await this.$confirm('亲，确定要删除吗?')
        await delUsers(id)
        this.$message.success('亲，已经删除了')
        this.getUsers()
      } catch (error) {
        console.log(error)
      }
    },
    formatEmployment(cellValue){
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue
      )
      return obj ? obj.value : '未知'
    }
  }
}
</script>

<style>
*{
  padding: 0;
  margin: 0;
}
.employess{
  display: flex;
  flex-direction: column;
}
.per-container{
  width: 1200px;
  display: flex;
  flex-direction: column;
}
.per-header{
  width: 100%;
  height: 60px;
  margin:20px;
  display: flex;
  border: 1px solid #ccc;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}
.per-left{
  color: blue;
  font-size: 20px;
  padding: 0 10px;
}
.em-main{
  margin-left: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}




</style>
